﻿Imports System.ComponentModel
Imports BusinessLayer
Imports Ocean.OceanFramework.MVVM
Imports Ocean.OceanFramework.CommonDialog
Imports BusinessEntityObjects
Imports System.Collections.ObjectModel
Imports Ocean.OceanFramework.WPF.Utility.Helpers

Public Class EditContactItemsControlSelectionViewModel
    Inherits ContainerFormViewModelBase

#Region " Declarations "

    Private _cmdDeleteCommand As ICommand
    Private _cmdNewCommand As ICommand
    Private _cmdSaveCommand As ICommand
    Private _cmdSelectCommand As ICommand
    Private _objAllContacts As List(Of ContactSearchResult)
    Private _objContactViewModel As ContactViewModel

#End Region

#Region " Properties "

    Public Property AllContacts() As List(Of ContactSearchResult)
        Get
            Return _objAllContacts
        End Get

        Private Set(ByVal Value As List(Of ContactSearchResult))
            _objAllContacts = Value
            OnPropertyChanged("AllContacts")
        End Set
    End Property

    Public Property ContactViewModel() As ContactViewModel
        Get
            Return _objContactViewModel
        End Get

        Private Set(ByVal Value As ContactViewModel)
            _objContactViewModel = Value
            OnPropertyChanged("ContactViewModel")
        End Set
    End Property

    Public Overrides ReadOnly Property IsDirty() As Boolean
        Get
            Return _objContactViewModel IsNot Nothing AndAlso _objContactViewModel.Contact IsNot Nothing AndAlso _objContactViewModel.Contact.IsDirty
        End Get
    End Property

#End Region

#Region " Command Properties "

    Public ReadOnly Property DeleteCommand() As ICommand
        Get

            If _cmdDeleteCommand Is Nothing Then
                _cmdDeleteCommand = New RelayCommand(Function() DeleteExecute(), AddressOf Me.CanDeleteExecute)
            End If

            Return _cmdDeleteCommand
        End Get
    End Property

    Public ReadOnly Property NewCommand() As ICommand
        Get

            If _cmdNewCommand Is Nothing Then
                _cmdNewCommand = New RelayCommand(Function() Me.NewExecute)
            End If

            Return _cmdNewCommand
        End Get
    End Property

    Public ReadOnly Property SaveCommand() As ICommand
        Get

            If _cmdSaveCommand Is Nothing Then
                _cmdSaveCommand = New RelayCommand(Function() Me.SaveExecute, AddressOf Me.CanSaveExecute)
            End If

            Return _cmdSaveCommand
        End Get
    End Property

    Public ReadOnly Property SelectCommand() As ICommand
        Get

            If _cmdSelectCommand Is Nothing Then
                _cmdSelectCommand = New RelayCommand(Of Integer)(AddressOf Me.SelectExecute)
            End If

            Return _cmdSelectCommand
        End Get
    End Property

#End Region

#Region " Constructor "

    Public Sub New()
        MyBase.ViewModelFriendlyName = "WPF Disciples"
        NewExecute()
        LoadWPFDisciples()
    End Sub

#End Region

#Region " Command Methods "

    Public Function CanSaveExecute(ByVal param As Object) As Boolean
        Return _objContactViewModel.Contact.ValidatationErrors.Count = 0 AndAlso Me.ContactViewModel.UIValidationErrorCount = 0 AndAlso Me.ContactViewModel.ErrorMessage.Length = 0
    End Function

    Public Function DeleteExecute() As Integer
        UpdateFocusedField()

        Dim strSaveActiveRuleSet As String = Me.ContactViewModel.Contact.ActiveRuleSet

        Try
            Me.ContactViewModel.Contact.ActiveRuleSet = "Delete"

            If Me.ContactViewModel.Contact.IsValid Then

                If ViewModelUIService.YesNoConfirmDelete = CustomDialogResult.Yes Then

                    If ContactBLL.CreateInstance.Delete(Me.ContactViewModel.Contact) = 1 Then
                        NewExecute()
                        ContactViewModel.WatermarkMessage = "Record deleted"
                        LoadWPFDisciples()
                        Return 1

                    Else
                        ViewModelUIService.MessageDialog("Delete Failure", "Delete Failed", "Could not delete contact")
                    End If

                End If

            Else
                Me.ContactViewModel.Contact.ActiveRuleSet = strSaveActiveRuleSet
                ContactViewModel.ErrorHeaderText = "Record invalid for delete"
            End If

        Catch ex As Exception
            ViewModelUIService.ExceptionDialog(ex)
        End Try
        '
        'if the delete failed or was cancelled, reset the ActiveRuleSet
        Me.ContactViewModel.Contact.ActiveRuleSet = strSaveActiveRuleSet
        Return 0
    End Function

    Public Function NewExecute() As Integer
        UpdateFocusedField()

        Try

            If _objContactViewModel Is Nothing OrElse VerifyDataBeforeClosing() Then
                LoadViewModel(BusinessLayer.ContactBLL.CreateInstance.Create)
                ContactViewModel.ErrorHeaderText = "Required Items"
                Return 1

            Else
                Return 0
            End If

        Catch ex As Exception
            ViewModelUIService.ExceptionDialog(ex)
        End Try

    End Function

    Public Overrides Function SaveExecute() As Integer
        UpdateFocusedField()

        If Me.ContactViewModel.Contact.IsDirty Then

            If Me.ContactViewModel.Contact.Id = 0 Then
                Return Insert()

            Else
                Return Update()
            End If

        End If

        Return -1
    End Function

    Public Sub SelectExecute(ByVal intId As Integer)
        UpdateFocusedField()

        If VerifyDataBeforeClosing() Then
            [Select](intId)
        End If

    End Sub

    Private Function CanDeleteExecute(ByVal param As Object) As Boolean

        If _objContactViewModel Is Nothing OrElse _objContactViewModel.Contact Is Nothing Then
            Return False
        End If

        Return _objContactViewModel.Contact.Id <> 0
    End Function

#End Region

#Region " Methods "

    Public Function Insert() As Integer

        Try

            If Me.ContactViewModel.Contact.IsValid Then

                Dim intNewId As Integer

                If ContactBLL.CreateInstance.Insert(Me.ContactViewModel.Contact, intNewId) = 1 Then
                    'this simulates getting the object fresh from the database after the insert, timestamp and other fields we may not have
                    LoadViewModel(ContactBLL.CreateInstance.Select(intNewId))
                    ContactViewModel.WatermarkMessage = "Record inserted"
                    LoadWPFDisciples()
                    Return 1
                End If

            End If

            ContactViewModel.ErrorHeaderText = "Save Errors"

        Catch ex As Exception
            ViewModelUIService.ExceptionDialog(ex)
        End Try

        Return 0
    End Function

    Public Function Search(ByVal strSearchString As String) As List(Of ContactSearchResult)
        Return ContactBLL.CreateInstance.SearchByLastName(strSearchString)
    End Function

    Public Sub [Select](ByVal intId As Integer)

        Try

            Dim obj As Contact = ContactBLL.CreateInstance.Select(intId)

            If obj IsNot Nothing Then
                LoadViewModel(obj)
                ContactViewModel.ErrorHeaderText = "Required Items"

            Else
                ViewModelUIService.MessageDialog("Selection Failed", "Selection Failed", String.Format("Could not select contact, Id:{0}", intId))
            End If

        Catch ex As Exception
            ViewModelUIService.ExceptionDialog(ex)
        End Try

    End Sub

    Public Function Update() As Integer

        Try

            Dim intSaveId As Integer = Me.ContactViewModel.Contact.Id

            If Me.ContactViewModel.Contact.IsValid Then

                If ContactBLL.CreateInstance.Update(Me.ContactViewModel.Contact) = 1 Then
                    LoadViewModel(ContactBLL.CreateInstance.Select(intSaveId))
                    ContactViewModel.WatermarkMessage = "Record saved"
                    LoadWPFDisciples()
                    Return 1
                End If

            End If

            ContactViewModel.ErrorHeaderText = "Save Errors"

        Catch ex As Exception
            ViewModelUIService.ExceptionDialog(ex)
        End Try

        Return 0
    End Function

    Private Sub LoadViewModel(ByVal obj As Contact)

        If _objContactViewModel Is Nothing Then
            Me.ContactViewModel = New ContactViewModel(obj)

        Else
            _objContactViewModel.Contact = obj
        End If

    End Sub

    Private Sub LoadWPFDisciples()
        Me.AllContacts = ContactBLL.CreateInstance.FillDropDown
    End Sub

#End Region

End Class
